#ifndef __CAN_IRQ_C__
#define __CAN_IRQ_C__

#include "can_var.c"

//when overflow interrupt triggered, print overflow information
void can_basic_dataoverflow()
{
	EO = 0;
	if(CAN_BASIC_STATUS & 0x02)
	{
	 	P0 = 'Y';
		test_info("OK_can basic_overflow status bit");
	}
	else 
	{
	 	P0 = 'N';
		test_info("FAIL_can basic_overflow status bit");
	}
	CAN_BASIC_COMMAND = 0x0C;			   	//release buffer & clear overrun flag
	counter_global += 1;				//times of overrun irq
	if((~CAN_BASIC_STATUS & 0x02))
	{
	 	P0 = 'Y';
		test_info("OK_can basic_clear overrun");
	}
	else 
	{
	 	P0 = 'N';
		test_info("FAIL_can basic_clear overrun");
	}
	
}

//when error interrupt triggered, print overflow information
void can_basic_error()
{
	char temp;
	temp = CAN_BASIC_STATUS;
	P0 = 'q';
}

//when trans_irq triggered, print information ,send more data or stop transfer
void can_basic_trans_irq()
{
	char xdata * data_ptr;

	EO = 0;
	if(counter_global <= 15)
	{
		CAN_TRANS_ID1 = accep_code_global;
		CAN_TRANS_ID2 = counter_global;		//set data_length

		data_ptr = trans_data_ptr_irq;

		CAN_TRANS_DATA1 = *data_ptr++;
		CAN_TRANS_DATA2 = *data_ptr++;
		CAN_TRANS_DATA3 = *data_ptr++;
		CAN_TRANS_DATA4 = *data_ptr++;
		CAN_TRANS_DATA5 = *data_ptr++;
		CAN_TRANS_DATA6 = *data_ptr++;
		CAN_TRANS_DATA7 = *data_ptr++;	
		CAN_TRANS_DATA8 = *data_ptr++;
	
		if(counter_global <= 8)
			trans_data_ptr_irq += counter_global;		//move data_ptr(global) to new data.
		else
			trans_data_ptr_irq += 8;
		counter_global += 1;

		CAN_BASIC_COMMAND = 0x01;		//transfer request;
	}
}

//when receive irq triggered, print receive information, read Rx buffer to ExRAM
void can_basic_receive_irq()
{
	char temp;
	char temp_data[8];
 	int i;

	EO = 0;
	temp = CAN_RECEIVE_ID2;
	temp &= 0x0F;
	if(temp > 8) temp = 8;		//read the length of data
	temp_data[0] = CAN_RECEIVE_DATA1;
	temp_data[1] = CAN_RECEIVE_DATA2;
	temp_data[2] = CAN_RECEIVE_DATA3;		
	temp_data[3] = CAN_RECEIVE_DATA4;
	temp_data[4] = CAN_RECEIVE_DATA5;
	temp_data[5] = CAN_RECEIVE_DATA6;
	temp_data[6] = CAN_RECEIVE_DATA7;
	temp_data[7] = CAN_RECEIVE_DATA8;
	for(i=0;i<temp;i++)
	{
	 	*received_data++ = temp_data[i];
	}
	CAN_BASIC_COMMAND = 0x04;		//release rx buffer 0000_0100;
	counter_global_2 ++;
}

//when overflow interrupt triggered, print overflow information
void can2_basic_dataoverflow()
{

}

//when error interrupt triggered, print overflow information
void can2_basic_error()
{

}

//when trans_irq triggered, print information ,send more data or stop transfer
void can2_basic_trans_irq()
{
	char xdata * data_ptr;

	EO = 0x80;

	if(counter_global <= 15)
	{
		CAN_TRANS_ID1 = accep_code_global;
		CAN_TRANS_ID2 = counter_global;		//set data_length

		data_ptr = trans_data_ptr_irq;

		CAN_TRANS_DATA1 = *data_ptr++;
		CAN_TRANS_DATA2 = *data_ptr++;
		CAN_TRANS_DATA3 = *data_ptr++;
		CAN_TRANS_DATA4 = *data_ptr++;
		CAN_TRANS_DATA5 = *data_ptr++;
		CAN_TRANS_DATA6 = *data_ptr++;
		CAN_TRANS_DATA7 = *data_ptr++;	
		CAN_TRANS_DATA8 = *data_ptr++;
	
		if(counter_global <= 8)
			trans_data_ptr_irq += counter_global;		//move data_ptr(global) to new data.
		else
			trans_data_ptr_irq += 8;
		counter_global += 1;
		
		CAN_BASIC_COMMAND = 0x01;		//transfer request;
	}

}

//when receive irq triggered, print receive information, read Rx buffer to ExRAM
void can2_basic_receive_irq()
{
	char temp;
	char temp_data[8];
	int i;

	EO = 0x80;
	temp = CAN_RECEIVE_ID2;
	temp &= 0x0F;
	if(temp > 8) temp = 8;		//read the length of data
	temp_data[0] = CAN_RECEIVE_DATA1;
	temp_data[1] = CAN_RECEIVE_DATA2;
	temp_data[2] = CAN_RECEIVE_DATA3;		
	temp_data[3] = CAN_RECEIVE_DATA4;
	temp_data[4] = CAN_RECEIVE_DATA5;
	temp_data[5] = CAN_RECEIVE_DATA6;
	temp_data[6] = CAN_RECEIVE_DATA7;
	temp_data[7] = CAN_RECEIVE_DATA8;
	for(i=0;i<temp;i++)
	{
	 	*received_data++ = temp_data[i];
	}
	CAN_BASIC_COMMAND = 0x04;		//release rx buffer 0000_0100;
	counter_global_2 ++;
}






void can_int(void) interrupt 8 
{
	char can_int;

	ECAN = 0;
	EO = 0;	
	can_int = CAN_BASIC_INTERRUPT;
	
	if (can_int & 0x08)	 //overflow
		can_basic_dataoverflow();
	if (can_int & 0x04)	 //error
		can_basic_error();
	if (can_int & 0x02)	 //trans done
		can_basic_trans_irq();
	if (can_int & 0x01)	 //receive ready
		can_basic_receive_irq();
	ECAN = 1;
}

void can2_int(void) interrupt 9 
{
 	char can2_int;

	ECAN2 = 0;	
	EO = 0x80;
	can2_int = CAN_BASIC_INTERRUPT;
	
	if (can2_int & 0x08)
		can2_basic_dataoverflow();
	if (can2_int & 0x04)
		can2_basic_error();
	if (can2_int & 0x02)
		can2_basic_trans_irq();
	if (can2_int & 0x01)
		can2_basic_receive_irq();
	ECAN2 = 1;
}


#endif